package myapps.myservlet;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SmilesLoading
 */
public class SmilesLoading extends HttpServlet 
{
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SmilesLoading() 
    {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException 
	{
		
		res.setContentType("text/html; charset=UTF-8");
		
        PrintWriter out = res.getWriter();
        
        out.println("<html>");
        out.println("<link href=\"style.css\" type=\"text/css\" rel=\"stylesheet\" />");
        out.println("<script type=\"text/javascript\" src=\"js/function.js\"></script>");
        out.println("<body>");        
        out.println("<table width=\"100%\" border=\"0\"><tr>");
        
        ServletContext context = getServletContext();  
        String path = context.getRealPath("data/smilies.txt");        
        FileInputStream in = new FileInputStream(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        int i=0;
        
        while((line = br.readLine())!= null)
        {
        	 i++;
            // out.println(line);
             String [] aStr = line.split(" => ");
             String aChar = aStr[0].replace("\"", "\\\"");
             aChar = aChar.replace("\\","\\\\");
             out.println("<td class=\"fr_2\"><a href=\'javascript:addsmile(\""+aChar+"\");\'><img src=\""+ aStr[1] +"\" border=0></a></td>");
             if(i%5==0)
             {
             	 out.println("</tr>");
             }
        }
                
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
		
	}//end doGet();
}
